package sw6.pc.logic;

import java.awt.geom.Path2D;
import java.io.*;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class SettingsParser {

	private static final String ROOT  = "settings";
	private static final String VISIBLE = "visible";
	private static final String MENU = "menu";
	
	private static final String INTEGER = "int";
	private static final String STRING = "string";
	private static final String BOOLEAN = "boolean";
	private static final String DOUBLE = "double";
	private static final String OBJECT = "object";
	
	private static final String VARIABLE_NAME = "varName";
	private static final String REAL_NAME = "realName";
	private static final String DESCRIPTION = "description";
	private static final String MENU_TITLE = "title";
	private static final String CLASS_PATH = "classPath";
	
	
	public static Menu LoadXML(String path) throws SAXException, IOException
	{
		// Setup the XML DOM parser
		DocumentBuilderFactory dbf 	= DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder	= null;
		try {
			docBuilder = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		// Define inputstream and a HTML / XML document
		InputStream inputStream = null;
		Document doc 			= null;

		File inFile = new File(path);
		inputStream = new FileInputStream(inFile);
		doc 		= docBuilder.parse(inputStream);
		
		Element start =  ((Element)((Element) doc.getElementsByTagName(ROOT).item(0)).getElementsByTagName(VISIBLE).item(0));
		Menu rootMenu = new Menu("ROOT", "ROOT");
		
		handleMenu(rootMenu, start);
		
		return rootMenu;
	}
	
	private static void handleMenu(Menu menu, Element menuElement)
	{
		NodeList nodes = menuElement.getChildNodes();
		
		ArrayList<Element> integers = new ArrayList<Element>();
		ArrayList<Element> strings = new ArrayList<Element>();
		ArrayList<Element> doubles = new ArrayList<Element>();
		ArrayList<Element> booleans = new ArrayList<Element>();
		ArrayList<Element> objects = new ArrayList<Element>();
		ArrayList<Element> menues = new ArrayList<Element>();
		
		for (int i = 0; i < nodes.getLength(); i++) {
			if (nodes.item(i).getNodeName().equalsIgnoreCase(INTEGER)) {
				integers.add((Element) nodes.item(i));
			}
			if (nodes.item(i).getNodeName().equalsIgnoreCase(STRING)) {
				strings.add((Element) nodes.item(i));
			}
			if (nodes.item(i).getNodeName().equalsIgnoreCase(DOUBLE)) {
				doubles.add((Element) nodes.item(i));
			}
			if (nodes.item(i).getNodeName().equalsIgnoreCase(BOOLEAN)) {
				booleans.add((Element) nodes.item(i));
			}
			if (nodes.item(i).getNodeName().equalsIgnoreCase(OBJECT)) {
				objects.add((Element) nodes.item(i));
			}
			if (nodes.item(i).getNodeName().equalsIgnoreCase(MENU)) {
				menues.add((Element) nodes.item(i));
			}
		}
		
		handleElements(integers, menu, INTEGER);
		handleElements(strings, menu, STRING);
		handleElements(doubles, menu, DOUBLE);
		handleElements(booleans, menu, BOOLEAN);
		handleElements(objects, menu, OBJECT);
		
		for (int i = 0; i < menues.size(); i++) {
			Element newMenuElement = (Element) menues.get(i);
			String realName = newMenuElement.getAttribute(MENU_TITLE);
			String description = newMenuElement.getAttribute(DESCRIPTION);
			
			Menu newMenu = new Menu(realName, description);
			menu.addMenu(newMenu);
			handleMenu(newMenu, newMenuElement);
		}
	}
	
	private static void handleElements(ArrayList<Element> nodeList, Menu menu, String type) {
		for (int i = 0; i < nodeList.size(); i++) {
			Element e = (Element)nodeList.get(i);
			String varName = e.getAttribute(VARIABLE_NAME);
			String realName = e.getAttribute(REAL_NAME);
			String description = e.getAttribute(DESCRIPTION);
			String classPath = e.getAttribute(CLASS_PATH);
			
			menu.addSetting(new Setting(type, realName, varName, description, classPath));
		}
	}
}